package view;

import java.awt.BorderLayout;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

public class ConsolePanel extends JPanel{

	 TextArea aTextArea = new TextArea();
	    PrintStream aPrintStream  =
	       new PrintStream(
	         new FilteredStream(
	           new ByteArrayOutputStream()));
	    
	public ConsolePanel(){
		setVisible(true);
		System.setOut(aPrintStream); // catches System.out messages
        System.setErr(aPrintStream); // catches error messages
        Toolkit tk = Toolkit.getDefaultToolkit();
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{380, 0};
        gridBagLayout.rowHeights = new int[]{160, 0};
        gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        setLayout(gridBagLayout);
        GridBagConstraints gbc_aTextArea = new GridBagConstraints();
        gbc_aTextArea.fill = GridBagConstraints.BOTH;
        gbc_aTextArea.gridx = 0;
        gbc_aTextArea.gridy = 0;
        add(aTextArea, gbc_aTextArea);
	}
	
	class FilteredStream extends FilterOutputStream {
        public FilteredStream(OutputStream aStream) {
            super(aStream);
          }

        public void write(byte b[]) throws IOException {
            String aString = new String(b);
            aTextArea.append(aString);
        }

        public void write(byte b[], int off, int len) throws IOException {
            String aString = new String(b , off , len);
            aTextArea.append(aString);
        }
    }
}
